package com.ruoyi.community.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.ServiceRules;
import com.ruoyi.community.mapper.ServiceRulesMapper;
import com.ruoyi.community.service.IRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RulesServiceImpl implements IRulesService {

    @Autowired
    private ServiceRulesMapper serviceRulesMapper;

    @Override
    public Map<String, List<ServiceRules>> getRulesList() {
        Long communityId = Long.valueOf(SecurityUtils.getCommunityId());
        System.out.println(communityId);
        List<ServiceRules> list = serviceRulesMapper.selectAllRules(communityId);
        List<ServiceRules> community = new ArrayList<>();
        List<ServiceRules> city = new ArrayList<>();
        List<ServiceRules> province  = new ArrayList<>();
        List<ServiceRules> country = new ArrayList<>();
        for(ServiceRules rule : list) {
            switch (rule.getType()) {
                case "物业级" :
                    community.add(rule);
                    break;
                case "城市级" :
                    city.add(rule);
                    break;
                case "省级" :
                    province.add(rule);
                    break;
                case "国家级" :
                    country.add(rule);
                    break;
                default:
                    break;
            }
        }
        Map<String, List<ServiceRules>> rulesMap = new HashMap<>();
        rulesMap.put("community", community);
        rulesMap.put("city", city);
        rulesMap.put("province", province);
        rulesMap.put("country", country);
        return rulesMap;
    }

    @Override
    public Integer addNewRule(ServiceRules serviceRules) {
        serviceRules.setCommunityId(Long.valueOf(SecurityUtils.getCommunityId()));
        serviceRules.setLogicDelete(false);
        serviceRules.setCreateTime(new Date());
        serviceRules.setModifyTime(new Date());
        Integer result = serviceRulesMapper.insertSelective(serviceRules);
        return result;
    }

    @Override
    public Integer updateRuleInfoById(ServiceRules serviceRules) {
        serviceRules.setModifyTime(new Date());
        int update = serviceRulesMapper.updateByPrimaryKey(serviceRules);
        return update;
    }

    @Override
    public Integer deleteRuleById(Long ruleId) {
        int result = serviceRulesMapper.deleteByPrimaryKey(ruleId);
        return result;
    }

    @Override
    public ServiceRules getRuleById(Long ruleId) {
        ServiceRules serviceRules = serviceRulesMapper.selectByPrimaryKey(ruleId);
        return serviceRules;
    }

}
